/**
 * Created by zhengrunqiang on 2016-12-19.
 */
myApp.controller("NumberCtrl", ['$scope', '$http', '$routeParams', '$restUrls', '$post', '$rootScope', function ($scope, $http, $routeParams, $restUrls, $post, $rootScope) {

    var currYear = (new Date()).getFullYear();
    var opt = {};
    opt.date = { preset: 'date' };
    opt.datetime = { preset: 'datetime' };
    opt.time = { preset: 'time' };
    opt.default = {
        theme: 'android-ics light', //皮肤样式
        display: 'modal', //显示方式
        mode: 'scroller', //日期选择模式
        dateFormat: 'yyyy-mm-dd',
        lang: 'zh',
        showNow: true,
        nowText: "今天",
        startYear: currYear - 70, //开始年份
        endYear: currYear //结束年份
    };



    $("#appDate2").mobiscroll($.extend(opt['date'], opt['default']));

    $scope.getCustomerHardWare = function () {
       $http({ method: "GET", url: $restUrls["getCustomerHardWare"], params: {} }).success(function (result) {
           if (result.success == 1) {
               $scope.hardware = result.data;
           }
       }).error(function (data, status, headers, config) {
       });
   }

    $scope.getCustomerHardWare();

    $scope.save = false;
    $scope.SaveHardWare = function (hardware) {
        if ($scope.save) {
            return;
        }
        if (!$scope.SaveForm.$valid) {
            $scope.showError = true;
            return;
        }

        if(hardware.hardware_code && hardware.hardware_code.length>0){
            if(!$scope.isBarcodeNo(hardware.hardware_code)){
                $.scojs_message('机身编码不合规则', $.scojs_message.TYPE_ERROR);
                return;
            }
        }


        var parameters = {
            hardware_code: hardware.hardware_code,
            hardware_name:hardware.hardware_name,
            buy_time:hardware.buy_time,
            buy_address:hardware.buy_address,
            network_id:hardware.network_id
        };
        $scope.save = true;
        $post($restUrls["saveHardWare"], parameters).success(function (result) {
            if (result.success == 1) {
                $scope.save = false;
                window.location.href =  "#/userinfo";
            }
            else {
                $scope.save = false;
                $.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
            }
        }).error(function (data, status, headers, config) {
            $scope.save = false;
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        })
    }
    
    
    $scope.isBarcodeNo = function (code) {
        if(code.length == 14){
           //表示第一位是数字，第二位是字母，从第三位到第十四位为数字
            return code.match(/\d{1}[a-lA-L]\d{12}/g);
        }else if(code.length== 9||code.length == 10||code.length== 11){
            //表示校验规则为全是数字
            var reg = new RegExp("^[0-9]*$");
            console.log(reg);
            return reg.test(code);
        }else{
            return false;
        }
    }


    $scope.getProductCateInfo = function () {
        $http({ method: "GET", url: $restUrls["getProductCateInfo"], params: {  } }).success(function (result) {
            if (result.success == 1) {
                $scope.categorys = result.data;
            }
        }).error(function (data, status, headers, config) {
        });
    }
    $scope.getProductCateInfo();

    $scope.ChooseCategory = function (category) {
        $scope.products = category.productDetailList;
        $scope.categoryshow=false;
        $scope.productshow=true;
    }
    $scope.ChooseProduct = function (product) {
        $scope.hardware.hardware_name = product.product_name;
        $scope.productshow=false;
        $scope.productmodelshow = false;
    }


    $scope.getAreaInfo = function () {
        $http({ method: "GET", url: $restUrls["getAreaInfo"], params: { id:0 } }).success(function (result) {
            if (result.success == 1) {
                $scope.areas = result.data;
            }
        }).error(function (data, status, headers, config) {
        });
    }
    $scope.getAreaInfo();

    $scope.ChooseArea = function (area) {
        $http({ method: "GET", url: $restUrls["getAreaInfo"], params: { id:area.areaid } }).success(function (result) {
            if (result.success == 1) {
                $scope.area2s = result.data;
                $scope.areashow = false;
                $scope.areashow2 = true;
            }
        }).error(function (data, status, headers, config) {
        });
    }

    $scope.ChooseArea2= function (area) {
        $http({ method: "GET", url: $restUrls["getAreaInfo"], params: { id:area.areaid } }).success(function (result) {
            if (result.success == 1) {
                $scope.area3s = result.data;
                $scope.areashow2 = false;
                $scope.areashow3 = true;
            }
        }).error(function (data, status, headers, config) {
        });
    }
    $scope.ChooseArea3 = function (area) {
        $http({ method: "GET", url: $restUrls["getNetworkInfo"], params: { areaid:area.areaid } }).success(function (result) {
            if (result.success == 1) {
                $scope.networks = result.data;
                $scope.areashow3 = false;
                $scope.areashow4 = true;
            }
        }).error(function (data, status, headers, config) {
        });
    }

    $scope.ChooseNetWork = function (network) {
        $scope.hardware.buy_address = network.network_name;
        $scope.hardware.network_id = network.id;
        $scope.areashow4 = false;
        $scope.networkmodelshow = false;
    }
}]);
